/*** 
 * @Author: 王耀泽
 * @Date: 2025-02-05 10:21:13
 * @LastEditTime: 2025-03-03 21:43:57
 * @LastEditors: aigis
 * @Description: 15201563666
 * @FilePath: \vue-cesium2\src\minxi\drawPlot\DrawPoint.js
 * @先看《地理信息导论第七版》
 */

class DrawPoint {
    constructor(arg) {
        // 确保 viewer 正确赋值
        if (!arg.viewer) {
            throw new Error('viewer 未传递');
        }
        this.viewer = arg.viewer;
        this.Cesium = arg.Cesium || Cesium;
        this.callback = arg.callback;
        this._point = null;  // 最后一个点
        this._pointData = null; // 点数据用于构造点
        this._entities = []; // 脏数据
    }

    //返回最后活动点
    get point() {
        return this._point;
    }

    //加载点
    loadPoint(data) {
        return this.createPoint(data);
    }

    //返回点数据用于加载点
    getData() {
        return this._pointData;
    }

    //开始绘制
    startCreate() {
        var $this = this;
        this.handler = new this.Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
        this.handler.setInputAction(function (evt) { //单机开始绘制
            var cartesian = $this.getCatesian3FromPX(evt.position);
            if (!cartesian) return;
            var point = $this.createPoint(cartesian);
            $this._pointData = cartesian;
            $this._point = point;
            if (typeof $this.callback == "function") {
                // $this.callback(point);
                const cartographic = $this.Cesium.Cartographic.fromCartesian(cartesian);
                const lon = $this.Cesium.Math.toDegrees(cartographic.longitude);
                const lat = $this.Cesium.Math.toDegrees(cartographic.latitude);
                const alt = cartographic.height;
                $this.callback({ point, lon, lat, alt });
            }
        }, $this.Cesium.ScreenSpaceEventType.LEFT_CLICK);
        this.handler.setInputAction(function () {
            //右键销毁实例
            $this.destroy();
        }, $this.Cesium.ScreenSpaceEventType.RIGHT_CLICK);
    }

    //创建点
    createPoint(cartesian) {
        var $this = this;
        var point = this.viewer.entities.add({
            position: cartesian,
            point: {
                pixelSize: 10,
                color: $this.Cesium.Color.YELLOW,
            }
        });
        $this._entities.push(point); //加载脏数据
        return point;
    }

    // 更新点
    updatePosition(cartesian, colorObj) {
        if (this._point) {
            this._point.position = cartesian;
            this._pointData = cartesian;
            if (colorObj) {
                // const { R, G, B, A } = colorObj;
                // console.log(R, G, B, A);
                // this._point.point.color = new this.Cesium.Color(
                //     R / 255,
                //     G / 255,
                //     B / 255,
                //     A / 255
                // );
                const { R, G, B, A } = colorObj;
                // 确保颜色分量值在 0 到 255 之间
                const r = Math.max(0, Math.min(255, R)) / 255;
                const g = Math.max(0, Math.min(255, G)) / 255;
                const b = Math.max(0, Math.min(255, B)) / 255;
                const a = Math.max(0, Math.min(1, A)); // 透明度值在 0 到 1 之间

                this._point.point.color = new this.Cesium.Color(r, g, b, a);
            }
        }
    }

    //销毁鼠标事件
    destroy() {
        if (this.handler) {
            this.handler.destroy();
            this.handler = null;
        }
    }

    //清空实体对象
    clear() {
        for (var i = 0; i < this._entities.length; i++) {
            this.viewer.entities.remove(this._entities[i]);
        }
        this._entities = [];
        this._point = null;
    }

    getCatesian3FromPX(px) {
        var cartesian;
        var ray = this.viewer.camera.getPickRay(px);
        if (!ray) return null;
        cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
        return cartesian;
    }
}

export default DrawPoint;
